package com.becca.cake.controller.companyEatApply;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.activity.CompanyEatApplyModel;
import com.becca.cake.service.companyEatApply.ICompanyEatApplyService;
import com.becca.cake.util.JsonUtil;

/**
 * 试吃申请管理control类
 * @copyright  尊星(上海)信息科技有限公司
 * @author yellow
 * @date 2016年7月20日 下午4:57:40
 */
@Controller
@RequestMapping("/companyEatApplyManager")
public class CompanyEatApplyAction extends BaseAction {
    private static final Logger     logger = LoggerFactory.getLogger(CompanyEatApplyAction.class);

    @Autowired
    private ICompanyEatApplyService iCompanyEatApplyService;

    /**
     * 查询试吃申请列表
     * @param companyEatApplyModel
     * @return
     */
    @RequestMapping(value = "/selectCompanyEatApply", method = RequestMethod.POST)
    @ResponseBody
    public String selectCompanyEatApply(CompanyEatApplyModel companyEatApplyModel) {
        Map<String, Object> map = new HashMap<String, Object>();
        PagingBean page = getInitPagingBean();
        map.put("pagingBean", page);
        map.put("companyEatApplyModel", companyEatApplyModel);
        try {
            // step1:查询试吃申请数据
            List<CompanyEatApplyModel> list = this.iCompanyEatApplyService
                .selectCompanyEatApply(map);
            // step2:获取试吃申请总条数
            Long count = this.iCompanyEatApplyService.selectCompanyEatApplyCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据id查询试吃申请对象
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectCompanyEatApplyById", method = RequestMethod.POST)
    @ResponseBody
    public String selectCompanyEatApplyById(@RequestParam(value = "id", required = false) Integer id) {
        try {
            CompanyEatApplyModel companyEatApplyModel = this.iCompanyEatApplyService
                .selectCompanyEatApplyById(id);
            return JsonUtil.writeObjectFormJSON(companyEatApplyModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 编辑试吃申请
     * @param companyEatApplyModel
     * @return
     */
    @RequestMapping(value = "/updCompanyEatApplyById", method = RequestMethod.POST)
    @ResponseBody
    public String updCompanyEatApplyById(CompanyEatApplyModel companyEatApplyModel) {
        try {
            this.iCompanyEatApplyService.updCompanyEatApplyById(companyEatApplyModel);
            return JsonUtil.returnJsonSuccess("保存试吃申请信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 企业试吃审核通过处理
     * @param companyEatApplyModel
     * @return
     * @author patty
     */
    @RequestMapping(value = "/companyEatApplyReviewPass", method = RequestMethod.POST)
    @ResponseBody
    public String companyEatApplyReviewPass(@RequestParam(value = "id", required = false) Integer id) {
        try {
            this.iCompanyEatApplyService.companyEatApplyReviewPass(id, this.getUserSeesion()
                .getTrueName());
            return JsonUtil.returnJsonSuccess("企业试吃审核通过!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 不通过处理
     * @param id
     * @param applyStatus
     * @param noPassReason
     * @return
     */
    @RequestMapping(value = "/companyApplyEatdNoPass", method = RequestMethod.POST)
    @ResponseBody
    public String companyApplyEatdNoPass(@RequestParam(value = "id", required = false) Integer id,

    @RequestParam(value = "noPassReason", required = false) String noPassReason) {
        try {
            this.iCompanyEatApplyService.companyApplyEatdNoPass(id, noPassReason);
            return JsonUtil.returnJsonSuccess("修改成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    @SuppressWarnings({ "rawtypes" })
    @RequestMapping(value = "/exportCompanyEatApplyToExcel", method = RequestMethod.GET)
    public void exportCompanyEatApplyToExcel(HttpServletRequest request,
                                             HttpServletResponse response,
                                             @RequestParam(value = "ids", required = false) String ids) {
        List list = this.iCompanyEatApplyService.selectCompanyEatApplyToExcel(ids);
        try {
            this.iCompanyEatApplyService.exportCompanyEatApplyToExcel(request, response, list);
        } catch (Exception e) {
            logger.error("企业试吃数据导出excel失败!");
        }
    }

    /**
     * 企业试吃确认联系
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateEatApplyConnection", method = RequestMethod.POST)
    @ResponseBody
    public String updateEatApplyConnection(@RequestParam(value = "id", required = false) Integer id) {
        try {
            this.iCompanyEatApplyService.updateEatApplyConnection(id, this.getUserSeesion()
                .getTrueName());
            return JsonUtil.returnJsonSuccess("操作成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 企业试吃下单操作
     * @param companyEatApplyModel
     * @return
     */
    @RequestMapping(value = "/companyEatApplyOrderPass", method = RequestMethod.POST)
    @ResponseBody
    public String companyEatApplyOrderPass(@RequestParam(value = "id", required = false) Integer id,
                                           @RequestParam(value = "goodsPriceSizeId", required = false) Integer goodsPriceSizeId,
                                           @RequestParam(value = "deliveryDate", required = false) String deliveryDate,
                                           @RequestParam(value = "deliveryTimes", required = false) String deliveryTimes
                                          )

    {
        try {
            this.iCompanyEatApplyService.companyEatApplyOrderPass(id, goodsPriceSizeId,
                deliveryDate, deliveryTimes, this.getUserSeesion().getTrueName());
            return JsonUtil.returnJsonSuccess("试吃下单处理成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

}
